:******************************************************************************
:* BCPOUT.CMD
:******************************************************************************
:* BCP Export Script
:******************************************************************************
:* Developer: Dmitry Frenkel dima.frenkel@ps.net
:* Tenet Application Architecture Team
:* (C) 2003, Perot Systems Corporation. All rights reserved.
:*
:*	Modified By: Tanya Romankova 
:*	Modified On: 10/21/2003
:*	Modification: Add parameters for DB user authorization. 
:******************************************************************************
:* Usage:
:* bcpout <ServerName> <Database> <Owner> <TableName> <Mode> <DestinationFolder> 
:* <ServerName> - name of the database server
:* <Database> - name of the database
:* <Owner> - owner of the object
:* <TableName> - name of table (or view) to export data from
:* <Mode> - export mode: N=native,C-character. Character files will be Unicode.
:* <DestinationFolder> - folder to place export file, must end with \
:* <UserName> - DB user name (If UserName and Password are not set, then use trusted connection)
:* <Password> - DB user password
:******************************************************************************
:* Script uses informationm provided to launch SQL Server bcp tool,
:* export data from designated table into file <DestinationFolder>\<TableName.dat> file
:******************************************************************************
:* Requires - bcp.exe
:******************************************************************************
:* Notes: Script uses trusted connection to log into SQL server if user name and 
:* password are not set
:******************************************************************************

@ECHO OFF
: 1. We need to have all parameters present
IF "%1" == "" GOTO ERRORPARAMS
IF "%2" == "" GOTO ERRORPARAMS
IF "%3" == "" GOTO ERRORPARAMS
IF "%4" == "" GOTO ERRORPARAMS
IF "%5" == "" GOTO ERRORPARAMS
IF "%6" == "" GOTO ERRORPARAMS

: 2. Use DB authorization or trusted connection
SET Login= 
IF "%7" == ""  (
    SET Login=-T
) ELSE (
    IF "%8" == "" GOTO ERRORPARAMS
    SET Login=-U%7 -P%8
)

: 3. We need to determine export mode and run bcp in this mode
SET Charset = 
IF /I "%5" == "C" (
    SET Charset=-c -CRAW
) ELSE (
    IF /I "%5" == "N" (
        SET Charset=-n
    ) ELSE (
        GOTO ERRORPARAMS
    )
)

: 4. Call bcp 
BCP %2.%3.%4 out %6%4.TABLE %Charset% %Login% -q -v -S%1
GOTO END

:ERRORPARAMS
@ECHO Usage: bcpout ServerName Database Owner TableName Mode DestinationFolder
@ECHO   [ServerName] - name of the database server
@ECHO   [Database] - name of the database
@ECHO   [Owner] - owner of the object
@ECHO   [TableName] - name of table (or view) to export data from
@ECHO   [Mode] - export mode: N=native,C-character. Character files will be Unicode.
@ECHO   [DestinationFolder] - folder to place export file, must end with \
@ECHO  To use trusted connection do not set UserName and Password parameters
@ECHO  [UserName] - DB user name 
@ECHO  [Password] - DB password
:END
